<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" style="height: 100%">
    <div class="layuimini-main" style="height: calc(100% - 10px);">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" name="organizationName" placeholder="机构名称" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" name="organizationCode" placeholder="机构编码" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbar">
            <div class="layui-btn-group">
                <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="add">
                    <i class="layui-icon">&#xe654;</i>
                </button>
                <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" lay-event="delete">
                    <i class="layui-icon">&#xe640;</i>
                </button>
            </div>
        </script>
        <div class="layui-row" style="height: calc(100% - 100px);">
            <div class="layui-col-md3" style="overflow-x: auto;overflow-y: hidden; height: calc(100% - 0px);border: 1px solid #e6e6e6; margin: 10px -1px -1px 0px;">
                <div id="menuTree" class="demo-tree"></div>
            </div>
            <div class="layui-col-md9">
                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
            </div>
        </div>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-xs data-count-edit" lay-event="edit">编辑</a>

            {{# if(d.status == 0 || d.status == null) { }}
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="enable">启用</a>
            {{# } }}
            {{# if(d.status == 1) { }}
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="disable">停用</a>
            {{# } }}
        </script>

    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'tree'], function () {
        var $ = layui.jquery,
            form = layui.form,
            tree = layui.tree,
            table = layui.table;
        const host = `${window.location.protocol}//${window.location.host}`;
        $.ajaxSetup({
            headers: {'Access-Token': sessionStorage.getItem('token'), 'userId': sessionStorage.getItem("userId")}
        });


        $.get(`${host}/organization/tree`, {'parentId': sessionStorage.getItem("user_organization_parentId")},(rest) => {
            if (rest && "00" === rest.code) {
                loadTree(rest.data);
            }
        });

        var parentId = null;
        function loadTree(treeData) {
           let orgTree = tree.render({
                elem: '#menuTree'
                ,data: treeData
                ,onlyIconControl: true  //是否仅允许节点左侧图标控制展开收缩
                ,showCheckbox: true
                ,isJump: true
                ,search: true
                ,click: function(obj){
                   parentId = obj.data.id;
                    $("#menuTree").find('.layui-tree-txt').attr("style", '');
                    $(obj.elem[0]).children('.layui-tree-entry').children('.layui-tree-main').children('.layui-tree-txt').attr("style","color:red");
                   loadTable(parentId);
                }
            });
        }
        loadTable(parentId);

        function loadTable(parentId) {
            table.render({
                title: '机构编码',
                elem: '#currentTableId',
                method: 'get',
                url: `${host}/organization/page`,
                toolbar: '#toolbar',
                defaultToolbar: ['filter', 'exports', 'print'],
                cols: [[
                    {type: "checkbox", width: 50},
                    {field: 'organizationCode', width: 150, title: '机构编码'},
                    {field: 'organizationName', width: 180, title: '机构名称'},
                    {
                        field: 'status', width: 180, title: '状态', templet: (d) => {
                            return d.status === 1 ? '启用' : '停用';
                        }
                    },
                    {field: 'createTime', width: 150, title: '创建时间'},
                    {field: 'remark', width: 180, title: '说明'},
                    {title: '操作', fixed: 'right', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                skin: 'line',
                parseData: (rest) => {
                    return {
                        "code": rest.code,
                        "msg": rest.msg,
                        "count": rest.data.total,
                        "data": rest.data.records
                    }
                },
                request: {
                    pageName: 'current',
                    limitName: 'size'
                },
                where: { 'searchParams' : JSON.stringify({'parentId': parentId}) }
            });
        }

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            data.field.parentId = parentId;
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: { current: 1 },
                where: { 'searchParams' : result }
            }, 'data');
            return false;
        });

        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                if (!parentId) {
                    layer.msg("请选择一个机构");
                    return false;
                }
                var index = layer.open({
                    title: '新增应用',
                    btn: ['确定','关闭'],
                    type: 2,
                    shade: 0.2,
                    maxmin:false,
                    shadeClose: false,
                    area: ['80%', '80%'],
                    content: `add.html?timestamp=${new Date().getTime()}`,
                    yes : (index, layero) => {
                        let addData = $(layero).find('iframe')[0].contentWindow.callbackData();
                        addData.parentId = parentId;
                        $.post(`${host}/organization/save`, addData, (rest) => {
                            if (rest && "00" === rest.code) {
                                layer.msg(rest.msg);
                                $(".layui-laypage-btn")[0].click();
                                layer.close(index);
                                loadTable(parentId)
                            } else {
                                layer.msg(rest.msg);
                            }
                        });
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        })

        table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '修改应用',
                    btn: ['确定','关闭'],
                    type: 2,
                    shade: 0.2,
                    maxmin:false,
                    shadeClose: false,
                    area: ['80%', '80%'],
                    content: 'edit.html',
                    success: (layero, index) => {
                        var body = layer.getChildFrame('body', index);
                        body.find('input[name="id"]').val(obj.data.id);
                        body.find('input[name="organizationCode"]').val(obj.data.organizationCode);
                        body.find('input[name="organizationName"]').val(obj.data.organizationName);
                        body.find('textarea[name="remark"]').val(obj.data.remark);
                    },
                    yes : (index, layero) => {
                        let editData = $(layero).find('iframe')[0].contentWindow.callbackData();
                        $.post("/organization/save", editData, (rest) => {
                            if (rest && "00" === rest.code) {
                                layer.msg(rest.msg);
                                $(".layui-laypage-btn")[0].click();
                                layer.close(index);
                            } else {
                                layer.msg(rest.msg);
                            }
                        });
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('是否确定删除', function (index) {
                    let id = obj.data.id;
                    $.post("/organization/delete", {'id': id}, (rest) => {
                        layer.msg(rest.msg);
                        if (rest && "00" === rest.code) {
                            obj.del();
                        }
                    })
                    layer.close(index);
                });
            } else if (obj.event === 'disable') {
                layer.confirm('是否确定停用', function (index) {
                    let id = obj.data.id;
                    $.post("/organization/disable", {'id': id}, (rest) => {
                        layer.msg(rest.msg);
                        $(".layui-laypage-btn")[0].click();
                    })
                    layer.close(index);
                });
            } else if (obj.event === 'enable') {
                layer.confirm('是否确定启用', function (index) {
                    let id = obj.data.id;
                    $.post("/organization/enable", {'id': id}, (rest) => {
                        layer.msg(rest.msg);
                        $(".layui-laypage-btn")[0].click();
                    })
                    layer.close(index);
                });
            }
        });
    });
</script>


</body>

<style>
    html, body {
        height: calc( 100% - 10px);
        width: calc( 100% - 10px);
    }
</style>
</html>